package com.zcfeng.ischool;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import io.rong.imkit.fragment.ConversationListFragment;
import java.util.ArrayList;
import java.util.List;
import Fragment.CampusFragment;
import Fragment.IschoolserviceFragment;
import io.rong.imkit.RongIM;
import io.rong.imkit.model.UIConversation;
import io.rong.imlib.model.Conversation;
import utils.ToolBarUtils;

public class MainActivity extends AppCompatActivity {
    LinearLayout tbar;
    ViewPager ischoolViewPager;
    ToolBarUtils tbu;
    String[] toolbarTittle;
    List<Fragment> ischoolFragments = new ArrayList<Fragment>();
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datainit();
        viewinit();
        textView.setText("我的校园");
        initlistiner();
    }

    private void viewinit() {
        textView = (TextView) findViewById(R.id.titleinfo);
        //初始化菜单
        tbar=(LinearLayout) findViewById(R.id.toolbar);
        //设置菜单按键名
        toolbarTittle=new String[]{"我的校园","消息"};
        //实例化菜单工具类
        tbu=new ToolBarUtils();
        //使用菜单工具类创建餐单按键
        tbu.createToolBar(tbar,toolbarTittle);
        //设置默认选中项为第一个
        tbu.changecolor(0);
    }

    private void datainit()
    {
        ConversationListFragment cf=new ConversationListFragment();
        Uri uri= Uri.parse("rong://" + getApplication().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")
                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")
                .build();
        cf.setUri(uri);
        RongIM.setConversationListBehaviorListener(new RongIM.ConversationListBehaviorListener(){

            @Override
            public boolean onConversationPortraitClick(Context context, Conversation.ConversationType conversationType, String s) {

                RongIM.getInstance().startConversation(context,conversationType,s,"null");
                return true;
            }

            @Override
            public boolean onConversationPortraitLongClick(Context context, Conversation.ConversationType conversationType, String s) {
                return false;
            }

            @Override
            public boolean onConversationLongClick(Context context, View view, UIConversation uiConversation) {
                return false;
            }

            @Override
            public boolean onConversationClick(Context context, View view, UIConversation uiConversation) {
                RongIM.getInstance().startConversation(context,uiConversation.getConversationType(),uiConversation.getConversationTargetId(),"null");
                return true;
            }
        });

        //初始化ViewPager
        ischoolViewPager=(ViewPager) findViewById(R.id.viewpager);
        ischoolFragments.add(new CampusFragment());
        ischoolFragments.add(cf);
        ischoolViewPager.setAdapter(new mydapter(getSupportFragmentManager()));

    }
    private void initlistiner() {
        ischoolViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            public void onPageSelected(int arg0) {
                //当viewpager中某一页被选中时改变菜单配色

                tbu.changecolor(arg0);
                textView.setText(toolbarTittle[arg0]);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        tbu.setOntbclick(new ToolBarUtils.onTBclick() {

            public void onTBClick(int position) {
                //按键点击时切换viewpager的页面
                textView.setText(toolbarTittle[position]);
                ischoolViewPager.setCurrentItem(position);
            }
        });
    }

    class mydapter extends FragmentPagerAdapter {

        public mydapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            return ischoolFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return ischoolFragments.size();
        }

    }
    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
